
import { Resource, invoke } from '@tauri-apps/api/core';

import { ConnectorDataSource } from '../../connector';
import { BlockEnum } from '@/components/workflow/types';
import Database from "@tauri-apps/plugin-sql";
import { create } from '@tauri-apps/plugin-fs';
import { homePath, createDirs, uuid, getG01DataBase, saveText } from '../../../utils';
import { FlowProject } from "../../types";


export enum WorkflowType {
    Agent = "agent",
    G01Server = "g01server",
    ChatFlow = "chatflow",
    DataFlow = "dataflow",
    Trait = "trait",
}

export const workflowExecute = async (file: string, args: { [key: string]: any }, options?: {
    compile: any,
    execute: any
}): Promise<any> => {
    console.log('workflowExecute:', args);
    let res: any = await invoke('plugin:mg-platform|workflow_execute', {
        file: file,
        args: args,
        compileOptions: options?.compile ? options?.compile : {},
        executeOptions: options?.execute ? options?.execute : {}
    });
    console.log(res);
    return res;
}

export const DEFAULT_EMPTY_WORKFLOW_DATA = {
    type: 'AGENT',
    conversation_variables: [],
    environment_variables: [],
    features: {
        file_upload: {
            "allowed_file_extensions": [
                ".JPG",
                ".JPEG",
                ".PNG",
                ".GIF",
                ".WEBP",
                ".SVG"
            ],
            "allowed_file_types": [
                "image"
            ],
            "allowed_file_upload_methods": [
                "local_file",
                "remote_url"
            ],
            "enabled": false,
            "fileUploadConfig": {
                "audio_file_size_limit": 50,
                "batch_count_limit": 5,
                "file_size_limit": 15,
                "image_file_size_limit": 10,
                "video_file_size_limit": 100,
                "workflow_file_upload_limit": 10
            },
            "image": {
                "enabled": false,
                "number_limits": 3,
                "transfer_methods": [
                    "local_file",
                    "remote_url"
                ]
            },
            "number_limits": 3
        },
        opening_statement: "",
        retriever_resource: {
            enabled: true
        },
        sensitive_word_avoidance: {
            enabled: false
        },
        speech_to_text: {
            "enabled": false
        },
        suggested_questions: [],
        suggested_questions_after_answer: {
            enabled: false
        },
        text_to_speech: {
            enabled: false,
            language: "",
            voice: ""
        }
    },
    graph: {
        nodes: [{
            data: {
                type: BlockEnum.Start,
                title: "开始",
                variables: []
            },
            type: 'custom',
            id: `${new Date().getTime()}`
        }],
        edges: []
    }
}

export const workflowCreateEmptyFile = async (identifier: string, filename: string): Promise<string | null> => {
    // let home = await homePath();
    // let exportPath = `${home}/flows/${identifier}/`;
    // await createDirs(exportPath);
    // let filename = `${exportPath}/workflow.json`;
    // await create(filename);

    await saveText(filename, JSON.stringify(DEFAULT_EMPTY_WORKFLOW_DATA));
    return filename;

}

export const flowCreateEmptyTraitFile = async (domainIdentifier: string, identifier: string): Promise<string | null> => {
    let home = await homePath();
    let exportPath = `${home}/traits/${domainIdentifier}/`;
    await createDirs(exportPath);
    let filename = `${exportPath}/${identifier}.json`;
    await create(filename);

    await saveText(filename, JSON.stringify(DEFAULT_EMPTY_WORKFLOW_DATA));
    return filename;

}

export const flowCreateEmptyObjectTraitFile = async (objectIdentifier: string, identifier: string): Promise<string | null> => {
    let home = await homePath();
    let exportPath = `${home}/objtraits/${objectIdentifier}/`;
    await createDirs(exportPath);
    let filename = `${exportPath}/${identifier}.json`;
    await create(filename);

    await saveText(filename, JSON.stringify(DEFAULT_EMPTY_WORKFLOW_DATA));
    return filename;

}